package com.cn.aam.checaiduo.ui.homepage.pay;

import android.content.Context;
import android.content.Intent;
import android.widget.TextView;

import com.cn.aam.checaiduo.AppData;
import com.cn.aam.checaiduo.Const;
import com.cn.aam.checaiduo.R;
import com.cn.aam.checaiduo.backend.RetrofitHome;
import com.cn.aam.checaiduo.backend.observer.OnRequestV2Listener;
import com.cn.aam.checaiduo.backend.rx.RxSchedulers;
import com.cn.aam.checaiduo.base.BaseActivity;
import com.cn.aam.checaiduo.base.ToolBarManager;
import com.cn.aam.checaiduo.bean.base.NormalResponseB;
import com.cn.aam.checaiduo.bean.pay.PayResultB;
import com.cn.aam.checaiduo.utils.MyViewsUtils;
import com.orhanobut.logger.Logger;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author 微木森林
 * @time 2017/6/1 下午5:02
 * @description 支付成功
 * @remark
 */
public class PayResultSuccessActivity extends BaseActivity {
    public static final String DATA_ORDER_ID = "data_order_id";//订单ID

    @BindView(R.id.tv_pay_amount)
    TextView tvPayAmount;
    @BindView(R.id.tv_order_serial_code)
    TextView tvOrderSerialCode;
    @BindView(R.id.tv_sequence)
    TextView tvSequence;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_date_time)
    TextView tvDateTime;

    private String orderId;

    public static Intent newInstance(Context context, String orderId) {
        Intent intent = new Intent(context, PayResultSuccessActivity.class);
        intent.putExtra(DATA_ORDER_ID, orderId);
        return intent;
    }

    @Override
    protected int layoutId() {
        return R.layout.activity_pay_result_success;
    }


    @Override
    protected void onGenerate() {
        orderId = getIntent().getStringExtra(DATA_ORDER_ID);
//        Logger.e("订单ID为" + orderId);
        RetrofitHome.getInstance()
                .getInternalApi()
                .payResult(AppData.getToken(), orderId)
                .compose(RxSchedulers.<NormalResponseB<PayResultB>>toMain())
                .subscribe(new OnRequestV2Listener<PayResultB>(this) {
                    @Override
                    public void onNext(NormalResponseB<PayResultB> value) {
                        super.onNext(value);
                        if (value.getIsLogged() != Const.STATE_LOGIN) {
                            goLogin();
                            return;
                        }
                        if (value.getCode() == Const.REQUEST_FAILED) return;
                        renderViews(value.getData());
                    }
                });

    }


    private void renderViews(PayResultB item) {
        tvPayAmount.setText(item.getOrder_amount() + "元");
        tvOrderSerialCode.setText(item.getOrder_no());
        tvSequence.setText("第" + item.getSequence() + "期");
        tvTitle.setText(item.getTitle());
        tvDateTime.setText(item.getPaid_time());
        MyViewsUtils.showAllViews(tvPayAmount, tvOrderSerialCode, tvSequence, tvTitle, tvDateTime);
    }

    @Override
    protected void initView() {
        //toolbar
        ToolBarManager.with(this, WMContent)
                .setBackgroundColor(R.color.toolbar_bg_grey)
                .setNavigationIcon(R.drawable.ic_nav_arrow_back)
                .setTitle("订单支付")
                .setTitleColor(R.color.black_normal);
        MyViewsUtils.hideAllViews(tvPayAmount, tvOrderSerialCode, tvSequence, tvTitle, tvDateTime);
    }

    @OnClick(R.id.btn_submit)
    public void onClickToBackHome() {
        AppData.closeActivityExceptMain();
        Intent intent = new Intent();
        intent.setAction(Const.BROADCAST_FRESH_INVEST_LIST);
        sendBroadcast(intent);
    }
}
